import { defineStore } from "pinia";
import { sensorDataApi, getStatusApi } from "@/api/monitor";
import { SensorData, StatusData } from "@/api/monitor/types";
import { store } from "@/store";

export const useMonitorStore = defineStore("monitor", () => {
  // state
  const monitorDataList = ref<Array<string>>([]); // 传感器数据集合
  const deviceStatus = ref(false);
  /**
   * 登录调用
   *
   * @param {LoginData}
   * @returns
   */
  function getSensorData(sensorData: SensorData) {
    return new Promise<any>((resolve, reject) => {
      sensorDataApi(sensorData)
        .then((response) => {
          // monitorDataList.value = response.data
          // deviceStatus.value = response
          resolve(response);
        })
        .catch((error) => {
          reject(error);
        });
    });
  }
  function getStatusData(statusData: StatusData) {
    return new Promise<any>((resolve, reject) => {
      getStatusApi(statusData)
        .then((response) => {
          // monitorDataList.value = response.data
          resolve(response);
        })
        .catch((error) => {
          reject(error);
        });
    });
  }

  return {
    monitorDataList,
    getSensorData,
    getStatusData,
  };
});
// 非setup
export function useMonitorStoreHook() {
  return useMonitorStore(store);
}
